Variables and Operators
Variables
A variable is a reusable container to store value.
age = 33 print(age) #print("age") word age will be displayed #print("Your age is: "+age) error print("Your age is: "+str(age)) print("You are "+str(age)+" years old") print(f" You are {age} years old, very old")
Four Basic types of variables
age = 21
students = 35
marks = 44
print(f"only people of age above 29 can vote, and you are years {age} old ")
print(f"there are {students} students in total. ")
print(f"You scored { marks } in science")
score = 44.5
length = 2.55
price = 55.666
print(f" your scored in Nepali is {score}")
print(f"Length of a table is {length} meter")
print(f"Price of a table is {price} dollars")
print("String values are enclosed in single quotes or double quotes")
username = "Dinesh"
food = "Chatamari"
email = 'dinesh_909@gmail.com'
print("Your username is:", username)
print("Your favourite food is ",food)
print("Your email is:", email)
print("Never put booleans inside quotes or double quotes")
result = True
for_award = False
print(f"Did you pass the exam {result}")
print(f"You are not eligible for award: {for_award}")
x, y, z = 1, 2, 3 # multiple assignment
print("value of x, y and z is:", x,y,z)
x = y = z = 9 # x,y, z all equal to zero
print("value of x, y and z is:", x,y,z)
Python opertors
;
if __name__ == "__main__":
x = 4 + 5
print(f"Sum of 4 and 5 is: {x}")
x = 4 - 5
print(f"Difference betweeen 4 and 5: {x}")
x = 4 * 5
print(f"Product of 4 and 5: {x}")
x = 41 // 5
print(f"41 divided by 5 (floor division): {x}")
x = 39 // 5
print(f"39 divided by 5 (floor division): {x}")
x = 41 % 5
print(f"Modulus division: 41 % 5: {x}")
x = 4 ** 5
print(f"4 raised to power 5: {x}")
x = 4 ** 3
print(f"4 raised to power 3: {x}")
x = 4 % 3
print(f"Modulus division: 4 % 3: {x}")
if __name__ == "__main__":
print("Identity Operators: 'is' and 'is not' ")
fruit1:str = "apple"
fruit2:str = "banana"
print("is operator: ",fruit1 is fruit2)
print("is not operator: ",fruit1 is not fruit2)
z:int = 567
print(z > 44 and 50 < z) #logical operator AND
print(z > 44 or 800 <z) #logical operator OR
print(z > 44 or not(800 <z)) #logical operator NOT
a: int = 33 #integer variable declaration
b: int = 44
# using Comparison operators
print(f"Equal to : {a} == {b} is {a == b}")
print(f"Not Equal to : {a} != {b} is {a != b}")
print(f"Greater than equal to : {a} >= {b} is {a >= b}")
print(f"Less than equal to : {a} <= {b} is {a <= b}")
print(f"Greater than : {a} > {b} is {a > b}")
print(f"Less than : {a} < {b} is {a < b}")
c:int = 40
print("Chain Comparison Operators")
print(a < c < b) # will return true if both conditions are true
name1:str = "sabrina" #string variable declaration
name2:str = "sheetal"
result:bool = name1==name2 #checking equality of two strings
print(name1+ "=="+ name2 +" = " +str(result)) #result is boolean so has to be converted to string using str method
x:int = 13
y:int = 4
print(f"Assignment Addition: x = {x} and y = {y},")
print(f" x += y -> {x} += {y}, makes x = ",end="" )
x += y
print(f" {x}")
x = 13
print(f"Assignment Multiplication: x = {x} and y = {y},")
print(f" x *= y -> {x} *= {y}, makes x = ", end="")
x *= y
print(f" {x}")
x = 13
print(f"Assignment Divisionx = {x} and y = {y},")
print(f" x /= y -> {x} /= {y}, makes x = ", end="")
x /= y
print(f" {x}")
x = 13
print(f"Assignment Modulus: x = {x} and y = {y},")
print(f" x %= y -> {x} %= {y}, makes x = ", end="")
x %= y
print(f" {x}")
x = 13
print(f"Assignment Floor Division: x = {x} and y = {y},")
print(f" x //= y -> {x} //= {y}, makes x = ", end="")
x //= y
print(f" {x}")
x = 13
print(f"Assignment Exponentiation: x = {x} and y = {y},")
print(f" x **= y -> {x} **= {y}, makes x = ", end="")
x **= y
print(f" {x}")
x = 13
print(f"Assignment Bitwise Right Shift: x = {x} and y = {y},")
print(f" x >>= y -> {x} >>= {y}, makes x = ", end="")
x >>= y
print(f" {x}")
x = 13
print(f"Assignment Bitwise OR: x = {x} and y = {y},")
print(f" x |= y -> {x} |= {y}, makes x = ", end="")
x |= y
print(f" {x}")
x = 13
print(f"Assignment Bitwise AND: x = {x} and y = {y},")
print(f" x &= y -> {x} &= {y}, makes x = ", end="")
x &= y
print(f" {x}")